﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using WenSkin.Controls;
using static WenSkin.WenXmlConfig.ConfigXmlParsing;

namespace WenSkin.WenXmlConfig
{
    public partial class WenConfigForm : WenSkin.Forms.WenForm
    {
        public WenConfigForm() : this("WenAutoConfig")
        {
        }
        public WenConfigForm(string xmlName)
        {
            InitializeComponent();
            ControlAdd();
            Text += " - " + xmlName;
            config = new ConfigXmlParsing(xmlName);

            ConfigAdd();
        }
        private readonly ConfigXmlParsing config;

        #region 委托事件

        [Category("Wen"), Description("按钮点击事件")]
        public event ConfigXmlItemClickEventHandler ConfigXmlItemClick;

        [Category("Wen"), Description("移除一项")]
        public event ConfigXmlItemEventHandler ConfigXmlItemRemove;

        [Category("Wen"), Description("新增一项")]
        public event ConfigXmlItemEventHandler ConfigXmlItemAdd;

        [Category("Wen"), Description("值变化")]
        public event ConfigXmlItemValueEventHandler ConfigXmlItemValueChanged;



        [ComVisible(true)]
        public delegate void ConfigXmlItemClickEventHandler(object sender, ConfigXmlItemClickEventArgs e);
        public class ConfigXmlItemClickEventArgs : ConfigXmlItemEventArgs
        {
            public ConfigXmlItemClickEventArgs(bool showTextBox, ConfigXmlGroup configXmlGroup, ConfigXmlItem configXmlItem) : base(configXmlGroup, configXmlItem)
            {
                ShowTextBox = showTextBox;
                ConfigXmlGroup = configXmlGroup;
                ConfigXmlItem = configXmlItem;
            }

            public bool ShowTextBox { get; set; }
        }

        [ComVisible(true)]
        public delegate void ConfigXmlItemValueEventHandler(object sender, ConfigXmlItemValueEventArgs e);
        public class ConfigXmlItemValueEventArgs : ConfigXmlItemEventArgs
        {
            public ConfigXmlItemValueEventArgs(ConfigXmlGroup configXmlGroup, ConfigXmlItem configXmlItem, string changeValue) : base(configXmlGroup, configXmlItem)
            {
                ChangeValue = changeValue;
                State = true;
            }
            public string ChangeValue { get; set; }
            public bool State { get; set; }
        }

        [ComVisible(true)]
        public delegate void ConfigXmlItemEventHandler(object sender, ConfigXmlItemEventArgs e);
        public class ConfigXmlItemEventArgs : EventArgs
        {
            public ConfigXmlItemEventArgs(ConfigXmlGroup configXmlGroup, ConfigXmlItem configXmlItem)
            {
                ConfigXmlGroup = configXmlGroup;
                ConfigXmlItem = configXmlItem;
            }
            public ConfigXmlGroup ConfigXmlGroup { get; set; }
            public ConfigXmlItem ConfigXmlItem { get; set; }
        }
        #endregion

        private void ControlAdd()
        {
            WenButton WenImageButton = new WenButton()
            {
                Text = "保存",
                Dock = DockStyle.Bottom,
            };
            WenImageButton.Click += WenImageButton_Click;
            this.Controls.Add(WenImageButton);
        }

        private void WenImageButton_Click(object sender, EventArgs e)
        {
            config.SaveXmlConfig();
            MsgBoxInformation("保存成功！部分设置需要重启软件才能生效！");
        }
        /// <summary>
        /// 保存设置
        /// </summary>
        public void SaveXmlConfig()
        {
            config.SaveXmlConfig();
        }

        private void ConfigAdd()
        {
            flowLayoutPanel1.Controls.Clear();
            foreach (ConfigXmlGroup gr in config.Group)
            {
                GroupBox groupBox = new GroupBox()
                {
                    Name = gr.Name,
                    Text = gr.Info,
                    AutoSize = true,
                    BackColor = Color.Transparent,
                    ForeColor = WenSkin.Controls.ControlHelper.ColorReverse(this.BackColor),
                    Visible=gr.Visible,
                };
                FlowLayoutPanel flow = new FlowLayoutPanel()
                {
                    Dock = DockStyle.Fill,
                    Size = new Size(450, 0),
                    AutoSize = true,
                    FlowDirection = FlowDirection.TopDown
                };
                groupBox.Controls.Add(flow);
                foreach (ConfigXmlItem item in gr.Item)
                {
                    ConfigXmlItemControl configXmlItemControl = new ConfigXmlItemControl(gr, item);
                    configXmlItemControl.ConfigXmlItemClick += (s, e) =>
                    {
                        //事件准发即可
                        ConfigXmlItemClick?.Invoke(this, e);
                    };
                    configXmlItemControl.ConfigXmlItemRemove += (s, e) =>
                    {
                        //移除内容界面需要更改，做处理
                        ConfigXmlItemRemove?.Invoke(this, e);
                        gr.Item.Remove(item);
                        config.SaveXmlConfig();
                        ConfigAdd();
                    };
                    configXmlItemControl.ConfigXmlItemValueChanged += (s, e) =>
                    {
                        ConfigXmlItemValueChanged?.Invoke(this, e);
                    };
                    flow.Controls.Add(configXmlItemControl);
                }
                //添加按钮
                if (gr.AllowAdd)
                {
                    flow.Controls.Add(allowAddButton(gr));
                }
                flowLayoutPanel1.Controls.Add(groupBox);
            }
        }

        #region 增添一个添加一项按钮
        private WenButton allowAddButton(ConfigXmlGroup group)
        {
            WenButton button = new WenButton()
            {
                Text = "新增一项",
                Size = new Size(500, 26)
            };
            button.Click += (s, e) =>
            {
                ConfigXmlItem configXmlItem = new ConfigXmlItem()
                {
                    Name = $"{group.Name}{group.Item.Count + 1}",
                    Info = "自动添加",
                    Ecc = false,
                    ReadOnly = false,
                    Remove = true,
                    Value = "",
                };

                WenSkin.Forms.WenForm form = new Forms.WenForm()
                {
                    StartPosition = FormStartPosition.CenterScreen,
                    Size = new Size(300, 100),
                    Text = "请输入名称"
                };

                TextBox textBox = new TextBox()
                {
                    Text = "新增名称",
                    Dock = DockStyle.Fill,
                    //Multiline = true,
                    ScrollBars = ScrollBars.Both,
                    WordWrap = false
                };

                WenButton button1 = new WenButton()
                {
                    Text = "确定",
                    Dock = DockStyle.Bottom,
                };
                button1.Click += (sx, ex) =>
                {
                    ConfigXmlItemAdd?.Invoke(this, new ConfigXmlItemEventArgs(group, configXmlItem));
                    configXmlItem.Info = textBox.Text;
                    group.Item.Add(configXmlItem);
                    config.SaveXmlConfig();
                    form.Dispose();
                    ConfigAdd();
                };

                form.Controls.Add(textBox);
                form.Controls.Add(button1);
                form.ShowDialog();
            };
            return button;
        }


        #endregion

        #region 单项样式

        [ToolboxItem(false)]
        private class ConfigXmlItemControl : WenSkin.Controls.WenControl
        {
            public ConfigXmlItemControl(ConfigXmlGroup gr, ConfigXmlItem item) : base()
            {
                this.Visible = item.Visible;
                Controls.WenLableTextBox wenChoiceTextBox = new Controls.WenLableTextBox()
                {
                    Dock = DockStyle.Fill,
                    Name = item.Name,
                    TextLable = item.Info,
                    Text = item.Value,
                    Password = item.Ecc,
                    ReadOnly = item.ReadOnly,
                };
                //按钮点击事件
                wenChoiceTextBox.ButtonClick += (s, e) =>
                {
                    ConfigXmlItemClickEventArgs ex = new ConfigXmlItemClickEventArgs(e.ShowTextBox, gr, item);
                    ConfigXmlItemClick?.Invoke(this, ex);
                    e.ShowTextBox = ex.ShowTextBox;
                };

                if (item.Ecc)
                    wenChoiceTextBox.Text = AES.AesEnDe.AESEncrypt(item.Value);
                if (item.Tip != null && item.Tip.Length != 0)
                {
                    foreach (Control c in wenChoiceTextBox.Controls)
                    {
                        tip.SetToolTip(c, item.Tip);
                    }
                }

                wenChoiceTextBox.Size = new Size(450, wenChoiceTextBox.Height);
                wenChoiceTextBox.TextBeforeChanged += (s, e) =>
                {
                    if (item.Ecc)
                    {
                        //先尝试解密，解密成功保存解密文件
                        if (AES.AesEnDe.AESDecrypt(wenChoiceTextBox.Text) is string text)
                        {
                            item.Value = text;
                            return;
                        }
                    }

                    //值改变响应事件
                    ConfigXmlItemValueEventArgs configXmlItemValueEventArgs = new ConfigXmlItemValueEventArgs(gr, item, wenChoiceTextBox.Text);

                    ConfigXmlItemValueChanged?.Invoke(this, configXmlItemValueEventArgs);

                    e.State = configXmlItemValueEventArgs.State;
                    if (configXmlItemValueEventArgs.State)
                        item.Value = wenChoiceTextBox.Text;
                };
                this.Controls.Add(wenChoiceTextBox);

                //移除项目 按钮
                if (gr.AllowAdd && item.Remove)
                {
                    WenButton button = new WenButton()
                    {
                        Image = Properties.Resources.close,
                        ImageSize = new Size(16, 16),
                        Dock = DockStyle.Right,
                        Size = new Size(20, 20)
                    };
                    button.Click += (s, e) =>
                    {
                        if (this.MsgBoxAsterisk("删除不可逆，是否删除"))
                        {
                            ConfigXmlItemRemove?.Invoke(this, new ConfigXmlItemEventArgs(gr, item));
                        }
                    };
                    this.Controls.Add(button);
                }
                this.Size = new Size(500, wenChoiceTextBox.Height);
            }

            #region 私有属性

            private readonly ToolTip tip = new ToolTip();

            #endregion

            #region 委托事件

            [Category("Wen"), Description("按钮点击事件")]
            public event ConfigXmlItemClickEventHandler ConfigXmlItemClick;

            [Category("Wen"), Description("移除一项")]
            public event ConfigXmlItemEventHandler ConfigXmlItemRemove;

            [Category("Wen"), Description("值变化")]
            public event ConfigXmlItemValueEventHandler ConfigXmlItemValueChanged;

            #endregion
        }

        #endregion
    }
}
